package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.data.GroupTypeBean;
import com.banzhi.shop.data.GroupTypeData;
import com.banzhi.shop.data.GroupingDetailsData;
import com.banzhi.shop.model.CallBack.ConfirmOrderTypeCallBack;
import com.banzhi.shop.model.IConfirmOrderTypeModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/7/20.
 */

public class ConfirmOrderTypeImpl implements IConfirmOrderTypeModel {
    @Override
    public void getConfirmOrderTypeData(String id, String type, final ConfirmOrderTypeCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.GroupTypeURL);
        if (!MyApp.loginMessage.getString("teamid", "").isEmpty()) {
         //   params.addBodyParameter("teamid",MyApp.loginMessage.getString("teamid", ""));
            params.addBodyParameter("tt",1+"");
        }
        params.addBodyParameter("id",id);
        params.addBodyParameter("type",type);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.e("ConfirmOrderTypeImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        GroupTypeData groupTypeData = gson.fromJson(result, GroupTypeData.class);
                        if (groupTypeData.getData()!= null) {
                            callBack.onGetConfirmOrderTypeDataSuccess(groupTypeData.getData());
                        }


                    }else {
                        callBack.onGetConfirmOrderTypeDataError(jsonObject);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("ConfirmOrderTypeImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(103)");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();

            }
        });

    }
}
